package com.ticket.sass.admin.util;

import com.ticket.sass.admin.handler.IdentityInput;
import com.ticket.sass.admin.manager.ThreadLocalIdInputHolderManager;

/**
 * @author ywb
 * @date 2024-03-23 11:08
 */
public class CinemaIdUtil {
    public static Long getCinemaIdFromRequest() {
        // 先尝试从请求头部获取
        String cinemaIdHeader = ResponseUtil.getRequest().getHeader("CinemaId");
        if (cinemaIdHeader != null && !cinemaIdHeader.isEmpty()) {
            try {
                return Long.valueOf(cinemaIdHeader); // 尝试将 CinemaId 转换为 Long 类型
            } catch (NumberFormatException e) {
                // 如果转换失败，继续尝试从 IdentityInput 获取
            }
        }
        
        // 如果头部没有或转换失败，再从 IdentityInput 获取
        IdentityInput value = ThreadLocalIdInputHolderManager.getValue();
        if (value != null) {
            Long cinemaId = value.getCinemaId(); // 假设 getCinemaId() 返回 Long 类型
            if (cinemaId != null) {
                return cinemaId;
            }
        }
        
        // 如果两者都没有，返回 0L
        return 0L;
    }
}
